//
//  AppView.m
//  应用管理
//
//  Created by 刘剑秋 on 15/3/7.
//  Copyright (c) 2015年 Gantry's App Home. All rights reserved.
//

#import "AppView.h"
#import "App.h"

/**
 *  类扩展
 */
@interface AppView()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *lable;

@property (weak, nonatomic) IBOutlet UIButton *downloadBtn;

@end

@implementation AppView


+ (instancetype)appViewInitWithApp:(App *)app {
    NSBundle * bundle = [NSBundle mainBundle];
    NSArray * _objs = [bundle loadNibNamed:@"AppView" owner:nil options:nil];
    AppView * _appView = [_objs lastObject];
    [_appView setApp:app];
    return _appView;
}

- (IBAction)down:(UIButton *)sender {
    [self.downloadBtn setTitle:@"已下载" forState:UIControlStateDisabled];
    self.downloadBtn.enabled = NO;
    
    UILabel * msg = [[UILabel alloc] init];
    msg.text = [NSString stringWithFormat:@"成功下载%@",_app.lableName];
    msg.backgroundColor = [UIColor colorWithRed:160/255.0 green:82/255.0 blue:45/255.0 alpha:1.0];
    msg.textColor = [UIColor whiteColor];
    msg.font = [UIFont systemFontOfSize:13];
    msg.textAlignment = NSTextAlignmentCenter;
    
    //设置圆角
    msg.layer.masksToBounds=YES;
    msg.layer.cornerRadius = 5;
    
    //设置文本大小
    NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    //size为可以显示的高大的宽和高，不可以超过
    CGSize msgSize = [msg.text boundingRectWithSize:self.superview.frame.size options:options attributes:@{NSFontAttributeName:msg.font} context:nil].size;
    NSLog(@"%f,%f",msgSize.width,msgSize.height);
    msg.frame = CGRectMake(0, 0, msgSize.width+20, msgSize.height+20);
    
    [self.superview addSubview:msg];
    msg.center = self.superview.center;

    //设置动画
    [UIView animateWithDuration:3.0 animations:^{
        msg.alpha = 0;
    } completion:^(BOOL finished) {
        [msg removeFromSuperview];
    }];

}

/**
 *  设置app的变量的值
 *
 *  @param app
 */
- (void)setApp:(App *)app {
    _app = app;
    _imageView.image = [UIImage imageNamed:_app.imageName];
    _lable.text = _app.lableName;
}


@end
